<#include "../common/common.html"/> <@layout>
<script type="text/javascript">
    var url,ids=[];
    /**
     * 打开新增模型的表单，录入信息
     */
    function newModel() {
        $('#dlg').dialog('setTitle', '新增模型').dialog('open');
        $('#myform').form('clear');
        $('#perms').tree({
            url:'${ctx}/authRole/permsTree',
            checkbox:true
        });
        url = '${ctx}/authRole/save';
    }
    /**
     * 编辑选中的模型信息
     */
    function editModel() {
        $('#myform').form('clear');

        var row = $('#dt-grid').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('setTitle', '修改模型').dialog('open');
            $('#myform').form('load', row);
            $('#perms').tree({
                url:'${ctx}/authRole/permsTree/'+row.id,
                checkbox:true
            });
            url = '${ctx}/authRole/update';
        } else {
            $.messager.show({
                title : '提示',
                msg : '请先选择一条记录，再进行修改！',
                timeout:2000
            });
        }
    }
    /**
     * 删除选中的模型信息
     */
    function deleteModel() {
        var row = $('#dt-grid').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', '您确定要删除选中的记录吗?', function(r) {
                if (r) {
                    $.post('${ctx}/authRole/delete/' + row.id, function(data) {
                        $.messager.show({
                            title : '提示',
                            msg : data,
                            timeout:2000
                        });
                        $('#dt-grid').datagrid('reload'); // reload the user data
                    },"text");
                }
            });
        } else {
            $.messager.show({
                title : '提示',
                msg : '请先选择一条记录，再进行删除！',
                timeout:2000
            });
        }
    }
    /**
     * 保存新增的模型数据
     */
    function saveModel() {
        ids = [];//清空值
        var nodes = $('#perms').tree("getChecked");
        var num = nodes.length;
        if (num < 1) {
            $.messager.confirm('提示消息', "您没有给该角色选择任何权限，是否继续？", function(r) {
                if (!r) {
                    return false;
                }
            });
        } else {
            for ( var i = 0; i < num; i++) {
                $.getNodes($('#perms'),nodes[i]);
            }
        }
        $('#permIds').val(ids.join(','));
        $('#myform').form('submit', {
            url : url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                $('#dlg').dialog('close');
                $('#dt-grid').datagrid('reload');
                $.messager.show({ // show error message
                    title : '提示',
                    msg : data,
                    timeout:2000
                });
            }
        });
    }
    /**
     * 根据查询条件进行查询，各条件需要加上query样式，以便函数自动提取查询参数
     */
    function queryModel() {
        $('#dt-grid').datagrid('load',$.getQueryParam('queryTable'));


    }
    /**
     * 取消grid组件的选中状态
     */
    function unCheckModel() {
        $('#dt-grid').datagrid('uncheckAll');
    }
</script>
<body class="easyui-layout">
<div data-options="region:'north'" style="height: 35px; padding: 2px">
    <table id='queryTable' cellpadding="0" cellspacing="0"
           class="form-table" align='center'>
        <tr>
            <td style="width: 100px">角色名称：</td>
            <td style="width: 180px"><input name="search_EQ_name"
                                            class="query" style="width: 150px" /></td>
            <td><a href="#" class="easyui-linkbutton"
                   data-options="iconCls:'icon-search'" onclick="queryModel()">查询</a></td>
        </tr>
    </table>
</div>
<div data-options="region:'center',border:false">
    <table id="dt-grid" class="easyui-datagrid"
           data-options="url:'${ctx}/authRole/query?orgid='+${orgid},fit:true,fitColumns:true,idField:'id',border:false,pagination:true,rownumbers:true,singleSelect:true,toolbar:'#toolbar'">
        <thead>
        <tr>
            <th field="name" width="100">角色名称</th>
            <th field="rmk" width="100">备注</th>
        </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-add',plain:'true'" onclick="newModel()">新增模型</a>
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-edit',plain:'true'"
           onclick="editModel()">编辑模型</a> <a href="#" class="easyui-linkbutton"
                                             data-options="iconCls:'icon-remove',plain:'true'"
                                             onclick="deleteModel()">删除模型</a> <a href="#"
                                                                                 class="easyui-linkbutton"
                                                                                 data-options="iconCls:'icon-undo',plain:'true'"
                                                                                 onclick="unCheckModel()">取消选中</a>
    </div>
</div>
<div id="dlg" class="easyui-dialog"
     data-options="closed:true,modal:true,buttons:'#dlg-buttons'"
     style="width: 600px; height: 450px;">
    <div class="easyui-layout" fit='true'>
        <form id="myform" method="post">
            <input type="hidden" name="id" /> <input type="hidden" id="permIds"
                                                     name="permIds" />
            <div data-options="region:'center',title:'角色'" style='padding: 10px'>
                <table cellpadding="0" cellspacing="0" class="form-table">
                    <tr>
                        <td style="width: 100px">角色名称:</td>
                        <td style="width: 200px"><input id="name" name="name"
                                                        type="text" class="easyui-validatebox"
                                                        data-options="validType:'maxLength[100]'" /></td>
                    </tr>
                    <td style="width: 100px">单位:</td>
                    <td style="width: 200px"><input id="orgid" name="orgid"
                                                    type="text" class="easyui-validatebox query"
                                                    data-options="validType:'maxLength[11]'" /></td>
                    <tr>
                        <td style="width: 100px">备注:</td>
                        <td style="width: 200px"><input id="rmk" name="rmk"
                                                        type="text" class="easyui-validatebox"
                                                        data-options="validType:'maxLength[200]'" /></td>
                    </tr>
                </table>
            </div>
            <div data-options="region:'east',title:'权限'"
                 style="width: 300px; padding: 10px">
                <ul id="perms" class="easyui-tree"></ul>
            </div>
        </form>
    </div>
    <div id="dlg-buttons" style="text-align: center">
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-save'" onclick="saveModel()">保存</a> <a
            href="#" class="easyui-linkbutton"
            data-options="iconCls:'icon-cancel'"
            onclick="javascript:$('#dlg').dialog('close')">取消</a>
    </div>
</div>
</body>
        <script>


            $(function(){
                $('#orgid').combotree({
                    url:"${ctx}/common/comboTree?table=auth_org&fields=id as id,name as text&pid=pid&pidStart=0&id=id"   ,
                    maxHeight : 500
                });
            });

        </script>
</@layout>
